<template>
  <el-form label-width="100px">
    <el-form-item label="SKU名称">
      <el-input placeholder="SKU名称" v-model="skuparams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input
        placeholder="价格(元)"
        type="number"
        v-model="skuparams.price"
      ></el-input>
    </el-form-item>
    <el-form-item label="重量(g)">
      <el-input
        placeholder="重量(g)"
        type="number"
        v-model="skuparams.weight"
      ></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input
        placeholder="SKU描述"
        type="textarea"
        v-model="skuparams.skuDesc"
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form inline="true" label-width="100px">
        <el-form-item
          v-for="item in attrArr"
          :key="item.id"
          :label="item.attrName"
        >
          <el-select style="width: 200px" v-model="item.attrAndValueId">
            <el-option
              :value="`${item.id}:${attrValue.id}`"
              v-for="attrValue in item.attrValueList"
              :key="attrValue.id"
              :label="attrValue.valueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form inline="true" label-width="100px">
        <el-form-item
          :label="item.saleAttrName"
          v-for="item in saleArr"
          :key="item.id"
        >
          <el-select style="width: 200px" v-model="item.saleIdAndValueId">
            <el-option
              :value="`${item.id}:${saleAttrValue.id}`"
              :label="saleAttrValue.saleAttrValueName"
              v-for="saleAttrValue in item.spuSaleAttrValueList"
              :key="saleAttrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table">
        <el-table-column
          type="selection"
          align="center"
          width="80px"
        ></el-table-column>
        <el-table-column label="图片">
          <template #="{ row }">
            <img
              :src="row.imgName"
              :alt="row.imgName"
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #="{ row }">
            <el-button type="primary" size="small" @click="handler(row)">
              设置默认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="default" @click="save">保存</el-button>
      <el-button type="primary" size="default" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
//引入请求的API
//获取平台属性
import { reqAttr } from '@/api/product/attr'
//获取照片墙的数据，以及SPU已有销售属性的数据
import { reqSpuImageList, reqSpuHasSaleAttr } from '@/api/product/spu'
//引入相关类型的定义
import type {
  SkuData,
  SpuData,
  SaleAttrResponseData,
  SpuHasImg,
  SaleAttr,
  SpuImg,
} from '@/api/product/spu/type'
import type { AttrReponseData, Attr } from '@/api/product/attr/type'
import { reactive, ref } from 'vue'
import { reqAddSku } from '@/api/product/spu'
import { ElMessage } from 'element-plus'

//存储平台属性
let attrArr = ref<Attr[]>([])
//销售属性
let saleArr = ref<SaleAttr[]>([])
//照片墙数据
let imgArr = ref<SpuImg[]>([])
//获取图片table组件实例
let table = ref<any>()

//收集SKU的参数
let skuparams = reactive<SkuData>({
  //父组件传过来的数据
  category3Id: '', //所属的三级分类的ID
  spuId: '', //已有的SPU的ID
  tmId: '', //SPU品牌的ID
  //v-model收集
  skuName: '', //sku名字
  price: '', //sku的价格
  weight: '', //sku的重量
  skuDesc: '', //sku的描述

  skuAttrValueList: [
    //平台属性的收集
  ],
  skuSaleAttrValueList: [
    //销售属性
  ],
  skuDefaultImg: '', //sku图片地址
})

//自定义事件
let $emit = defineEmits(['changeScene'])

//取消按钮的回调
const cancel = () => {
  $emit('changeScene', { flag: 0, params: 'update' })
}

//SKU当前子组件的方法对外暴露
const initSkuData = async (
  c1Id: number | string,
  c2Id: number | string,
  spu: SpuData,
) => {
  //收集数据
  skuparams.category3Id = spu.category3Id
  skuparams.spuId = spu.id as number
  skuparams.tmId = spu.tmId
  //初始化数据
  skuparams.skuName = ''
  skuparams.price = ''
  skuparams.weight = ''
  skuparams.skuDesc = ''

  //获取平台属性
  let result: AttrReponseData = await reqAttr(c1Id, c2Id, spu.category3Id)
  //获取对应的销售属性
  let result1: SaleAttrResponseData = await reqSpuHasSaleAttr(spu.id as number)
  //获取照片墙的数据
  let result2: SpuHasImg = await reqSpuImageList(spu.id as number)

  //存储
  //平台属性
  attrArr.value = result.data
  //销售属性
  saleArr.value = result1.data
  //图片
  imgArr.value = result2.data
}

//设置默认图片方法的回调
const handler = (row: SpuImg) => {
  //点击的时候，全部图片的复选框不勾选
  imgArr.value.forEach((item) => {
    table.value.toggleRowSelection(item, false)
  })
  //复选框选中
  table.value.toggleRowSelection(row, true)
  //收集图片地址
  skuparams.skuDefaultImg = row.imgUrl as string
}

//保存按钮方法的回调
const save = async () => {
  //整理参数
  //平台属性
  skuparams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrAndValueId) {
      let [attrId, valueId] = next.attrAndValueId.split(':')
      prev.push({ attrId, valueId })
    }
    return prev
  }, [])
  //销售属性
  skuparams.skuSaleAttrValueList = saleArr.value.reduce(
    (prev: any, next: any) => {
      if (next.saleIdAndValueId) {
        let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
        prev.push({ saleAttrId, saleAttrValueId })
      }
      return prev
    },
    [],
  )
  let result = await reqAddSku(skuparams)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '添加SKU成功',
    })
    //通知父组件切换场景为0
    $emit('changeScene', { flag: 0, params: 'update' })
  } else {
    ElMessage({
      type: 'error',
      message: '添加SKU失败',
    })
  }
}

//子组件对外暴露方法
defineExpose({ initSkuData })
</script>

<style scoped></style>
